package com.bdk.middle.base.web;


import com.bdk.middle.base.api.domain.FileResourcesDO;
import com.bdk.middle.base.api.query.FileResourcesQueryDO;
import com.bdk.middle.base.service.FileResourcesService;
import com.bdk.middle.common.domain.PageDO;
import com.bdk.middle.common.result.ResultDO;
import com.bdk.middle.common.result.web.ObjectResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Reagan
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/file-resources")
public class FileResourcesController {

    @Autowired
    private FileResourcesService FileResourcesService;

    @RequestMapping(value="/add.html", method = RequestMethod.POST)
    public @ResponseBody
    ObjectResponse add(@RequestBody FileResourcesDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = FileResourcesService.add(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/edit.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse edit(@RequestBody FileResourcesDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = FileResourcesService.edit(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/delete.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse delete(@RequestBody FileResourcesDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = FileResourcesService.remove(domain.getId(), null);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/detail.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse detail(@RequestBody FileResourcesDO domain) {

        ObjectResponse<FileResourcesDO> response = new ObjectResponse();

        ResultDO result = FileResourcesService.get(domain.getId());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((FileResourcesDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    @RequestMapping(value="/list.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse list(@RequestBody FileResourcesQueryDO domain) {

        ObjectResponse<PageDO<FileResourcesDO>> response = new ObjectResponse();

        ResultDO result = FileResourcesService.find(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((PageDO<FileResourcesDO>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }
}

